package com.example.demo.service.impl;

import com.example.demo.mapper.CouponMapper;
import com.example.demo.model.entity.Coupon;
import com.example.demo.model.entity.Goods;
import com.example.demo.model.enums.ResultCodeEnum;
import com.example.demo.model.request.coupon.CreateCouponRequest;
import com.example.demo.model.request.coupon.DeleteCouponRequest;
import com.example.demo.model.request.coupon.UpdateCouponRequest;
import com.example.demo.model.response.PageQueryCouponResponse;
import com.example.demo.model.response.PageQueryGoodsResponse;
import com.example.demo.service.CouponService;
import com.example.demo.util.PageInfo;
import com.example.demo.util.PageUtil;
import com.example.demo.util.PromotionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Override
    public Boolean create(CreateCouponRequest request) {
        //满减金额不能大于总金额
        if(request.getDecPrice() > request.getTotalPrice()) {
            throw PromotionException.fail(ResultCodeEnum.COUPON_MONEY_INVALID);
        }
        Coupon coupon = buildCoupon(request);
        couponMapper.insert(coupon);
        return true;
    }

    @Override
    public Boolean update(UpdateCouponRequest request) {
        Coupon existCoupon = couponMapper.selectById(request.getId());
        Coupon coupon = setCoupon(existCoupon, request);
        couponMapper.update(coupon);
        return true;
    }

    @Override
    public Boolean delete(DeleteCouponRequest request) {
        couponMapper.delete(request.getId());
        return true;
    }

    @Override
    public PageQueryCouponResponse pageQuery(PageInfo pageInfo) {
        PageQueryCouponResponse response = new PageQueryCouponResponse();

        //分页查询商品
        Integer offset = pageInfo.getPageSize() * (pageInfo.getCurrent() - 1);
        List<Coupon> couponList = couponMapper.pageQuery(pageInfo.getPageSize(), offset);
        response.setItems(couponList);

        //查询总记录条数,并设置分页信息
        long totalItem = couponMapper.getCount();
        setPageInfo(response, totalItem, pageInfo.getCurrent(), pageInfo.getPageSize());
        return response;
    }

    @Override
    public Coupon query(Integer id) {
        Coupon existCoupon = couponMapper.selectById(id);
        if ( null == existCoupon) {
            throw PromotionException.fail(ResultCodeEnum.Coupon_NOT_EXIST);
        }
        return existCoupon;
    }

    /**
     * 设置response的分页信息
     *
     * @param response
     * @param totalItem
     * @param current
     * @param pageSize
     */
    private void setPageInfo(PageQueryCouponResponse response, long totalItem, int current, int pageSize) {
        response.setTotal(totalItem);
        response.setCurrent(current);
        response.setPageSize(pageSize);
        response.setTotalPage(PageUtil.calculateTotalPage(totalItem, pageSize));
    }

    private Coupon buildCoupon(CreateCouponRequest request) {
        Coupon coupon = new Coupon();
        coupon.setName(request.getName());
        coupon.setTotalPrice(request.getTotalPrice());
        coupon.setDecPrice(request.getDecPrice());
        return coupon;
    }

    private Coupon setCoupon(Coupon coupon, UpdateCouponRequest request) {
        if (null != request.getName()) {
            coupon.setName(request.getName());
        }
        if (null != request.getTotalPrice()) {
            coupon.setTotalPrice(request.getTotalPrice());
        }
        if (null != request.getDecPrice()) {
            coupon.setDecPrice(request.getDecPrice());
        }
        return coupon;
    }
}
